//
//  WLReadingController.m
//  KnowEveryThings
//
//  Created by lanou3g on 16/5/5.
//  Copyright © 2016年 王玲. All rights reserved.
//

#import "WLReadingController.h"
#import "WLTableViewController.h"
#import "AppDelegate.h"
#import "UIImage+image.h"
#import "WLNetWorkRequestManager.h"
#import "WLModel.h"
#import "TableViewCell.h"
@interface WLReadingController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *readArr;
@end

@implementation WLReadingController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"阅读";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0
, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    self.navigationController.navigationBar.tintColor = [UIColor redColor];

//    设置订阅图标(及订阅图标样式)
//    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightBarButtonAction)];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginaName:@"subscribe.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBarButtonAction)];
    //将订阅图标加到视图
  self.navigationItem.rightBarButtonItem = item1;
    
    //
    self.navigationItem.titleView = [[UISegmentedControl alloc]initWithItems:@[@"推荐阅读",@"我的订阅"]];
    
    [self jieXi];
    
 //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
}
//数据解析
-(void)jieXi{
    
    [WLNetWorkRequestManager requestURL:[NSURL URLWithString:@"http://c.3g.163.com/recommend/getSubDocPic?from=yuedu&size=20&passport=&devId=Okb1CYzHSTmHU9Ai9RC6Kg%3D%3D&lat=l%2BVNjs3zzl%2FbaQI7nwh8jA%3D%3D&lon=tvp93sN1EjV2OcWZyBKZ9Q%3D%3D&version=7.0&net=wifi&ts=1462433505&sign=RyCKflu3%2Fqz6XXkgHK4eBic4YaKdiIMXVP%2FlREoy2St48ErR02zJ6%2FKXOnxX046I&encryption=1&canal=QQ_news"] Method:WL_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        if (data) {
            NSMutableDictionary *jion = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            NSArray *dataDict = jion[@"推荐"];
            
            for(NSDictionary *itemDict in dataDict){
                
                WLModel *wlModel = [WLModel new];
                
                [wlModel setValuesForKeysWithDictionary:itemDict];
                
                [self.readArr addObject:wlModel];

            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];

            });
        }
    }];
}


//右侧按钮方法
- (void)rightBarButtonAction{
    NSLog(@"右侧");
    
    WLTableViewController *wlVC = [[WLTableViewController alloc]init];
    [self.navigationController pushViewController:wlVC animated:YES];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.readArr.count;
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
//    WLModel *model = [[WLModel alloc] init];
    cell.readModel = self.readArr[indexPath.row];
    return cell;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 173;
}
#pragma mark----页面跳转

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
